<?php
class AdminuserController extends Controller {
	public function index() {
		$this->getSmarty ();
		// $this->smarty->assign ( 'page_no', $page_no );
		// $this->smarty->assign ( 'page_html', $page_html );
		$this->smarty->display ( "admin/users/adminuser.tpl" );
	}
	public function checkUser() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'AdminuserService.class.php';
		$userService = new UserService ( $this->getDB () );
		$name = $_POST ["name"];
	}
	public function getallrole() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'AdminuserService.class.php';
		$userService = new AdminuserService ( $this->getDB () );
		$roles = $userService->getallrole ();
		echo json_encode ( $roles );
	}
	public function addOrModify() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'AdminuserService.class.php';
		if (! CommonBase::isPost ()) {
			$this->smarty->display ( "admin/ad/adsite.tpl" );
			return;
		}
		$userService = new AdminuserService ( $this->getDB () );
		$id = $_POST ["id"];
		if (empty ( $id )) {
			$input_data = array (
					'user_name' => $_POST ["user_name"],
					'passwd' => md5 ( $_POST ["passwd"] ),
					'real_name' => $_POST ["real_name"],
					'mobile' => $_POST ["mobile"],
					'email' => $_POST ["email"],
					'user_desc' => $_POST ["user_desc"],
					'role_id' => $_POST ["role_id"],
					'user_image' => $_POST ["user_image"] 
			);
			$user_id = $userService->addAdUser ( $input_data );
		} else {
			$user = $userService->getAdminByID ( $id );
			if (! empty ( $user )) {
				$passwd = $user->passwd;
				if ( $_POST ["passwd"] != $passwd) {
					$input_data = array (
							'user_name' => $_POST ["user_name"],
							'passwd' => md5 ( $_POST ["passwd"] ),
							'real_name' => $_POST ["real_name"],
							'mobile' => $_POST ["mobile"],
							'email' => $_POST ["email"],
							'user_desc' => $_POST ["user_desc"],
							'role_id' => $_POST ["role_id"],
							'user_image' => $_POST ["user_image"] 
					);
				} else {
					$input_data = array (
							'user_name' => $_POST ["user_name"],
							'real_name' => $_POST ["real_name"],
							'mobile' => $_POST ["mobile"],
							'email' => $_POST ["email"],
							'user_desc' => $_POST ["user_desc"],
							'role_id' => $_POST ["role_id"],
							'user_image' => $_POST ["user_image"] 
					);
				}
				
				$input_condition = array (
						'id' => $_POST ["id"] 
				);
				
				$user_id = $userService->edit ( $input_data, $input_condition );
			}
		}
		echo "1";
	}
	public function delete() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'AdminuserService.class.php';
		$userService = new AdminuserService ( $this->getDB () );
		$id = $_POST ['id'];
		$user_id = $userService->deleteUser ( $id );
		echo 1;
	}
	function test($username, $password) {
		echo 'this is a test function ,username:' . $username . ',password:' . $password;
	}
	function getpage() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'AdminuserService.class.php';
		// START 数据库查询及分页数据
		$userService = new AdminuserService ( $this->getDB () );
		
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$offset = ($page - 1) * $rows;
		$result = array ();
		$row_count = $userService->countNum ();
		$result ["total"] = $row_count;
		$user_infos = $userService->userPage ( $offset, $rows );
		$items = array ();
		// while($row = $user_infos->fetch_assoc()){
		// print ($row);
		// array_push($items, $row);
		// }
		$result ["rows"] = $user_infos;
		echo json_encode ( $result );
		// $this->smarty->display("admin/user_list.tpl");
	}
}